package com.sc.VenusQueryBeta.ui.activites.login;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.listener.DialogUIListener;
import com.hengjun.kingstar.sdk.base.BasePresenter;
import com.hengjun.kingstar.sdk.base.activity.BaseMVPCompatActivity;
import com.hengjun.kingstar.sdk.utils.LogUtils;
import com.hengjun.kingstar.sdk.utils.SpUtils;
import com.hengjun.kingstar.sdk.utils.ToastUtils;
import com.hengjun.kingstar.sdk.widgets.TipHelper;
import com.sc.VenusQueryBeta.R;
import com.sc.VenusQueryBeta.constant.Constants;
import com.sc.VenusQueryBeta.contract.login.FirstLoginContract;
import com.sc.VenusQueryBeta.global.MyApplication;
import com.sc.VenusQueryBeta.model.bean.login.EquipmentLoginBean;
import com.sc.VenusQueryBeta.model.bean.login.FirstLoginSubmintBean;
import com.sc.VenusQueryBeta.presenter.login.FirstLoginPresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class FirstLoginActivity extends BaseMVPCompatActivity<FirstLoginContract.LoginPresenter> implements FirstLoginContract.ILoginView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.btn_equipment)
    com.hengjun.kingstar.sdk.widgets.CountDownButton send;
    @BindView(R.id.l)
    LinearLayout l;
    @BindView(R.id.relativeLayout)
    RelativeLayout relativeLayout;
    @BindView(R.id.btn_affirm)
    Button btnAffirm;
    private String countryCode;
    private String deviceUid;
    private String tvCountryCode;
    private String phoneet;
    private String pwd;

    @Override
    protected void initData() {
        super.initData();
        if (getIntent().getExtras() != null) {
            countryCode = getIntent().getExtras().getString(Constants.COUNTRY_CODE, "");
            deviceUid = getIntent().getExtras().getString(Constants.DIVICEUID, "");
            tvCountryCode = getIntent().getExtras().getString(Constants.TVCOUNTRYCODE, "");
            phoneet = getIntent().getExtras().getString(Constants.PHONEET, "");
            pwd = getIntent().getExtras().getString(Constants.PWD, "");
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_first_login;
    }

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return FirstLoginPresenter.newInstance();
    }

    @Override
    public void showNetworkError() {
        hideProgressDialog();
    }

    @Override
    public void showFirstBUserLoginCodeSuccess(EquipmentLoginBean equipmentLoginBean) {
        send.start();
        hideProgressDialog();
        SpUtils.putString(MyApplication.app, Constants.VERTIFICATION_CODE, equipmentLoginBean.getRandomNum());//保存驗證碼
        LogUtils.e("getRandomNum=" + equipmentLoginBean.getRandomNum());
//        ToastUtils.showToast("" + equipmentLoginBean.getRandomNum());
        LogUtils.e("phone==" + equipmentLoginBean.getRegisterSmsnum());
        LogUtils.e("设备ID2" + equipmentLoginBean.getDeviceUID());
        // showUsersLoginDataSuccess(usersLoginBean.getReturnCod());
        LogUtils.e("第一次登录获取验证码" + equipmentLoginBean.getClientUID() + "  " + equipmentLoginBean.getDeviceUID() + "  " + equipmentLoginBean.getDeviceType() + "  " + equipmentLoginBean.getRegisterSmsnum() +
                "  " + equipmentLoginBean.getAreaCode());

        DialogUIUtils.showAlert(this, "验证码", equipmentLoginBean.getRandomNum(), "", "", "确定", "", true, true, true, new DialogUIListener() {
            @Override
            public void onPositive(CharSequence input1, CharSequence input2) {

            }

            @Override
            public void onNegative(CharSequence input1, CharSequence input2) {

            }
        }).show();
    }

    @Override
    public void showFirstSumbitDataSuccess(FirstLoginSubmintBean firstLoginSubmintBean) {
        hideProgressDialog();
        LogUtils.e("getDeviceUID()==" + firstLoginSubmintBean.getDeviceUID() + "firstLoginSubmintBean.getReturnCod()=" + firstLoginSubmintBean.getReturnCod());
        LogUtils.e("第一次登录确认成功" + firstLoginSubmintBean.getId() + "  " + firstLoginSubmintBean.getSmsCode() + "  " + firstLoginSubmintBean.getPassword() + " " + firstLoginSubmintBean.getClientUID() +
                "  " + firstLoginSubmintBean.getDeviceUID() + "  " + firstLoginSubmintBean.getDeviceType());
        ToastUtils.showToast("已確認！");
        finish();
    }


    @OnClick({R.id.btn_equipment, R.id.btn_affirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_equipment:
                if (send.isFinish()) {//完成才調用
//                    if (et_e.getText().toString().trim().equals("")) {
                    showProgressDialog("正在發送");
                    if (countryCode.equals("1")) {
                        mPresenter.getFirstBUserLoginCode(phoneet, deviceUid, "android", phoneet); //flag=0/1 0就是第一次注册时传
                    } else {
                        mPresenter.getFirstBUserLoginCode(Constants.MEG + tvCountryCode + phoneet, deviceUid, "android", tvCountryCode + phoneet); //flag=0/1 0就是第一次注册时传
                    }
                } else {
                    ToastUtils.showToast("別著急哦");
                }
                break;
            case R.id.btn_affirm:
                //这里判断是否倒计时结束，避免在倒计时时多次点击导致重复请求接口
                if (!editText.getText().toString().trim().equals("")) {//
                    if (editText.getText().toString().trim().equals(SpUtils.getString(this, Constants.VERTIFICATION_CODE, ""))) {//驗證嗎一樣
                        if (countryCode.equals("1")) {
                            showProgressDialog("正在确认");
                            mPresenter.firstSumbit(phoneet, Constants.MEG, pwd, Constants.MEG + phoneet, deviceUid, "android");
                        } else {
                            showProgressDialog("正在確認");
                            mPresenter.firstSumbit(tvCountryCode + phoneet, Constants.MEG, pwd, Constants.MEG + tvCountryCode + phoneet, deviceUid, "android");
                        }
                    } else {
                        ToastUtils.showToast("验证码不一致");
                        showNetworkError();
                    }

                } else {
                    Animation shake = AnimationUtils.loadAnimation(mContext, R.anim.shake);//加载动画资源文件
                    editText.startAnimation(shake);
                    TipHelper.Vibrate(this, 100);
                    ToastUtils.showToast("驗證碼不能空！");
                }
                break;
        }
    }


}
